package com.zhfi.dataapply.schedule.writer.management;

import com.zhfi.dataapply.entity.data.po.management.ReportPO;
import com.zhfi.dataapply.entity.data.vo.management.ReportVO;
import com.zhfi.dataapply.mapper.management.*;
import com.zhfi.dataapply.util.BaseMapperUtil;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class ReportWriter implements ItemWriter<ReportVO> {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private ReportChangeRecordMapper reportChangeRecordMapper;
    @Autowired
    private ReportEquityChangeInfoMapper reportEquityChangeInfoMapper;
    @Autowired
    private ReportOutboundInvestmentMapper reportOutboundInvestmentMapper;
    @Autowired
    private ReportOutGuaranteeInfoMapper reportOutGuaranteeInfoMapper;
    @Autowired
    private ReportShareholderMapper reportShareholderMapper;
    @Autowired
    private ReportWebInfoMapper reportWebInfoMapper;

    @Override
    public void write(List<? extends ReportVO> list) throws Exception {
        for (ReportVO vo : list) {
            ReportPO reportPO = new ReportPO();
            BeanUtils.copyProperties(vo, reportPO);
            BaseMapperUtil.updateOrInsert(reportMapper, reportPO);
            vo.getChangeRecords().forEach(po -> BaseMapperUtil.updateOrInsert(reportChangeRecordMapper, po));
            vo.getEquityChangeInfos().forEach(po -> BaseMapperUtil.updateOrInsert(reportEquityChangeInfoMapper, po));
            vo.getOutboundInvestments().forEach(po -> BaseMapperUtil.updateOrInsert(reportOutboundInvestmentMapper, po));
            vo.getOutGuaranteeInfos().forEach(po -> BaseMapperUtil.updateOrInsert(reportOutGuaranteeInfoMapper, po));
            vo.getShareholders().forEach(po -> BaseMapperUtil.updateOrInsert(reportShareholderMapper, po));
            vo.getWebInfos().forEach(po -> BaseMapperUtil.updateOrInsert(reportWebInfoMapper, po));
        }
    }
}
